#!/usr/bin/envpython
# -*-coding:UTF-8-*-
'''
@File    :   __init__.py.py
@Contact :   308711822@qq.com
@License :   (C) Copyright 2021-2225, Personal exclusive right.

@Modify Time      @Author    @Version    @Desciption
------------      -------    --------    -----------
2023/2/7 13:48   小钟同学      1.0         None
'''
from typing import Union

from fastapi import Request
from fastapi.exceptions import RequestValidationError
from pydantic import ValidationError

from infirmary_admin_src.infirmary_common.infirmary_errors.validation_error import APIValidationError

# from infirmary_src.infirmary_common.infirmary_errors.validation_error import APIValidationError

# 常见的错误模板映射
_any_error = {
    'value_error.jsondecode': '传入的参数字段出现转换错误，请核对是否存在类型错误',
    'value_error.missing': '{loction} 类型 {name} 参数字段为必传参数',
    'value_error.any_str.max_length': '{loction} 类型 {name} 参数字段字符串长度最多{limit_value}位',
    'value_error.any_str.min_length': '{loction} 类型 {name} 参数字段字符串长度至少{limit_value}位以上',
    'value_error.number.not_gt': '{loction} 类型 {name} 参数字段信息必须大于{limit_value}',
    'value_error.number.not_le': '{loction} 类型 {name} 参数字段信息必须小于等于{limit_value}',
    # =============================================
    # type_error.none.not_allowed
    'type_error.none.not_allowed': '{loction} 类型 {name} 参数字段值必须不能空值',
    "type_error.integer": "{loction} 类型 {name} 参数必须是一个int类型的参数",
    "type_error.float": "{loction} 类型 {name} 参数必须是一个float类型的参数",
}


def get_any_en_error(_request: Request, exc: Union[RequestValidationError, ValidationError]):
    undefined = "{loction} type for: {name} is: {meessage} ,error type is：{errortypr}"
    print("输出！！",exc.body)
    for err in exc.errors():
        print("输出！errerr！", err)
    try:
        for err in exc.errors():
            print("11111111111111111111")
            if err["type"] == 'value_error.jsondecode':
                err["msg"] = _any_error[err["type"]]
                print("输出錯誤信息！！", err["msg"])
                return err["msg"]
            loction = err["loc"][0]
            try:
                name = err["loc"][1]
            except Exception:
                name = '未知参数字段，可能提交格式有误'
            _value = None
            try:
                _value = err['ctx']
            except:
                pass
            limit_value = None
            if _value and 'limit_value' in _value:
                limit_value = _value['limit_value']
            if limit_value:
                err["msg"] = _any_error[err["type"]].format(loction=loction, name=name,
                                                            limit_value=limit_value) if err[
                                                                                            "type"] in _any_error else \
                    undefined.format(loction=loction, name=name, meessage=err["msg"], errortypr=err["type"])
            else:
                err["msg"] = _any_error[err["type"]].format(loction=loction, name=name) if err[
                                                                                               "type"] in _any_error else \
                    undefined.format(loction=loction, name=name, meessage=err["msg"], errortypr=err["type"])
            return err["msg"]
    except:
        print("输出錯誤信息222222234444444444423333333333333322222！",)
        return APIValidationError.from_pydantic_only_one_message(exc)


value_error_error_msg_unwal = {'value_error.none.not_allowed': 'value_error.none.not_allowed',
                               'value_error.none.allowed': 'value_error.none.allowed',
                               'value_error.const': 'value_error.const',
                               'value_error.not_none': 'value_error.not_none',
                               'value_error.url': 'value_error.url',
                               'value_error.url.scheme': 'value_error.url.scheme',
                               'value_error.url.userinfo': 'value_error.url.userinfo',
                               'value_error.url.host': 'value_error.url.host',
                               'value_error.url.port': 'value_error.url.port',
                               'value_error.url.extra': 'value_error.url.extra',
                               'value_error.enum_instance': 'value_error.enum_instance',
                               'value_error.int_enum_instance': 'value_error.int_enum_instance',
                               'value_error.enum': 'value_error.enum',
                               'value_error.path.not_exists': 'value_error.path.not_exists',
                               'value_error.path.not_a_file': 'value_error.path.not_a_file',
                               'value_error.path.not_a_directory': 'value_error.path.not_a_directory',
                               'value_error.tuple.length': 'value_error.tuple.length',
                               'value_error.list.min_items': 'value_error.list.min_items',
                               'value_error.list.max_items': 'value_error.list.max_items',
                               'value_error.list.unique_items': 'value_error.list.unique_items',
                               'value_error.set.min_items': 'value_error.set.min_items',
                               'value_error.set.max_items': 'value_error.set.max_items',
                               'value_error.frozenset.min_items': 'value_error.frozenset.min_items',
                               'value_error.frozenset.max_items': 'value_error.frozenset.max_items',
                               'value_error.any_str.min_length': 'value_error.any_str.min_length',
                               'value_error.any_str.max_length': 'value_error.any_str.max_length',
                               'value_error.str.regex': 'value_error.str.regex',
                               'value_error.number.not_gt': 'value_error.number.not_gt',
                               'value_error.number.not_ge': 'value_error.number.not_ge',
                               'value_error.number.not_lt': 'value_error.number.not_lt',
                               'value_error.number.not_le': 'value_error.number.not_le',
                               'value_error.number.not_multiple': 'value_error.number.not_multiple',
                               'value_error.decimal.not_finite': 'value_error.decimal.not_finite',
                               'value_error.decimal.max_digits': 'value_error.decimal.max_digits',
                               'value_error.decimal.max_places': 'value_error.decimal.max_places',
                               'value_error.decimal.whole_digits': 'value_error.decimal.whole_digits',
                               'value_error.date.not_in_the_past': 'value_error.date.not_in_the_past',
                               'value_error.date.not_in_the_future': 'value_error.date.not_in_the_future',
                               'value_error.uuid.version': 'value_error.uuid.version',
                               'value_error.arbitrary_type': 'value_error.arbitrary_type',
                               'value_error.class': 'value_error.class', 'value_error.subclass': 'value_error.subclass',
                               'value_error.json': 'value_error.json',
                               'value_error.regex_pattern': 'value_error.regex_pattern',
                               'value_error.dataclass': 'value_error.dataclass',
                               'value_error.payment_card_number.digits': 'value_error.payment_card_number.digits',
                               'value_error.payment_card_number.luhn_check': 'value_error.payment_card_number.luhn_check',
                               'value_error.payment_card_number.invalid_length_for_brand': 'value_error.payment_card_number.invalid_length_for_brand',
                               'value_error.discriminated_union.missing_discriminator': 'value_error.discriminated_union.missing_discriminator',
                               'value_error.discriminated_union.invalid_discriminator': 'value_error.discriminated_union.invalid_discriminator'}

error_msg = {'field required': 'field required', 'extra fields not permitted': 'extra fields not permitted',
             'none is not an allowed value': 'none is not an allowed value', 'value is not none': 'value is not none',
             'value is not None': 'value is not None',
             'value could not be parsed to a boolean': 'value could not be parsed to a boolean',
             'byte type expected': 'byte type expected', 'value is not a valid dict': 'value is not a valid dict',
             'value is not a valid email address': 'value is not a valid email address',
             'invalid or missing URL scheme': 'invalid or missing URL scheme',
             'URL scheme not permitted': 'URL scheme not permitted',
             'userinfo required in URL but missing': 'userinfo required in URL but missing',
             'URL host invalid': 'URL host invalid',
             'URL host invalid, top level domain required': 'URL host invalid, top level domain required',
             'URL port invalid, port cannot exceed 65535': 'URL port invalid, port cannot exceed 65535',
             'URL invalid, extra characters found after valid URL: {}': 'URL invalid, extra characters found after valid URL: {}',
             '{} is not a valid Enum instance': '{} is not a valid Enum instance',
             '{} is not a valid IntEnum instance': '{} is not a valid IntEnum instance',
             'value is not a valid integer': 'value is not a valid integer',
             'value is not a valid float': 'value is not a valid float',
             'value is not a valid path': 'value is not a valid path',
             'file or directory at path "{}" does not exist': 'file or directory at path "{}" does not exist',
             'path "{}" does not point to a file': 'path "{}" does not point to a file',
             'path "{}" does not point to a directory': 'path "{}" does not point to a directory',
             'ensure this value contains valid import path or valid callable: {}': 'ensure this value contains valid import path or valid callable: {}',
             'value is not a valid sequence': 'value is not a valid sequence',
             'value is not a valid list': 'value is not a valid list',
             'value is not a valid set': 'value is not a valid set',
             'value is not a valid frozenset': 'value is not a valid frozenset',
             'value is not a valid tuple': 'value is not a valid tuple',
             'wrong tuple length {}': 'wrong tuple length {}',
             'ensure this value has at least {} items': 'ensure this value has at least {} items',
             'ensure this value has at most {} items': 'ensure this value has at most {} items',
             'the list has duplicated items': 'the list has duplicated items',
             'ensure this value has at least {} characters': 'ensure this value has at least {} characters',
             'ensure this value has at most {} characters': 'ensure this value has at most {} characters',
             'str type expected': 'str type expected',
             'string does not match regex "{}"': 'string does not match regex "{}"',
             'ensure this value is greater than {}': 'ensure this value is greater than {}',
             'ensure this value is greater than or equal to {}': 'ensure this value is greater than or equal to {}',
             'ensure this value is less than {}': 'ensure this value is less than {}',
             'ensure this value is less than or equal to {}': 'ensure this value is less than or equal to {}',
             'ensure this value is a multiple of {}': 'ensure this value is a multiple of {}',
             'value is not a valid decimal': 'value is not a valid decimal',
             'ensure that there are no more than {} digits in total': 'ensure that there are no more than {} digits in total',
             'ensure that there are no more than {} decimal places': 'ensure that there are no more than {} decimal places',
             'ensure that there are no more than {} digits before the decimal point': 'ensure that there are no more than {} digits before the decimal point',
             'invalid datetime format': 'invalid datetime format', 'invalid date format': 'invalid date format',
             'date is not in the past': 'date is not in the past',
             'date is not in the future': 'date is not in the future', 'invalid time format': 'invalid time format',
             'invalid duration format': 'invalid duration format',
             'value is not a valid hashable': 'value is not a valid hashable',
             'value is not a valid uuid': 'value is not a valid uuid',
             'uuid version {} expected': 'uuid version {} expected',
             'instance of {} expected': 'instance of {} expected', 'a class is expected': 'a class is expected',
             'subclass of {} expected': 'subclass of {} expected', 'Invalid JSON': 'Invalid JSON',
             'JSON object must be str, bytes or bytearray': 'JSON object must be str, bytes or bytearray',
             'Invalid regular expression': 'Invalid regular expression',
             'instance of {}, tuple or dict expected': 'instance of {}, tuple or dict expected',
             '{} is not callable': '{} is not callable',
             'value is not a valid IPv4 or IPv6 address': 'value is not a valid IPv4 or IPv6 address',
             'value is not a valid IPv4 or IPv6 interface': 'value is not a valid IPv4 or IPv6 interface',
             'value is not a valid IPv4 or IPv6 network': 'value is not a valid IPv4 or IPv6 network',
             'value is not a valid IPv4 address': 'value is not a valid IPv4 address',
             'value is not a valid IPv6 address': 'value is not a valid IPv6 address',
             'value is not a valid IPv4 network': 'value is not a valid IPv4 network',
             'value is not a valid IPv6 network': 'value is not a valid IPv6 network',
             'value is not a valid IPv4 interface': 'value is not a valid IPv4 interface',
             'value is not a valid IPv6 interface': 'value is not a valid IPv6 interface',
             'value is not a valid color: {}': 'value is not a valid color: {}',
             'value is not a valid boolean': 'value is not a valid boolean',
             'card number is not all digits': 'card number is not all digits',
             'card number is not luhn valid': 'card number is not luhn valid', 'Length for a {}': 'Length for a {}',
             'could not parse value and unit from byte string': 'could not parse value and unit from byte string',
             'could not interpret byte unit: {}': 'could not interpret byte unit: {}',
             'Discriminator {} is missing in value': 'Discriminator {} is missing in value',
             'No match for discriminator {})': 'No match for discriminator {})'
             }
